package com.woniu34.user.service.impl;

import com.woniu.common.constant.Constants;
import com.woniu.common.enums.ExceptionCode;
import com.woniu.common.exception.CustomException;
import com.woniu.common.utils.sign.Md5Utils;
import com.woniu34.user.entity.User;
import com.woniu34.user.mapper.UserMapper;
import com.woniu34.user.service.UserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;



    @Override
    public User findByUserAccountAndPassword(String userAccount, String password) {
        //把用户传入的明文密码加密后，传给数据库比较
        String encodedPassword = Md5Utils.hash(password + Constants.SALT);
        return userMapper.findByUserAccountAndPassword(userAccount,encodedPassword);
    }

    @Override
    public String queryVerifyCode(String cellphone) {

        //生成验证码
        int mobile_code = (int)((Math.random()*9+1)*100000);
        System.out.println("生成的验证码："+mobile_code);

        //发消息给rabbitmq
        Map<String,String> msg = new HashMap<>();
        msg.put("cellphone",cellphone);
        msg.put("verifyCode",String.valueOf(mobile_code));
        rabbitTemplate.convertAndSend(Constants.MQ_CELLPHONE_VERIFYCODE_EXCHANGE,Constants.MQ_CELLPHONE_VERIFYCODE_ROUTING_KEY,msg);

        //把产生的验证码放在redis中，并且设置5分钟超时时间
        ValueOperations operations = redisTemplate.opsForValue();
        operations.set(Constants.REGISTRY_CELLPHONE_VERIFYCODE+cellphone,String.valueOf(mobile_code),5, TimeUnit.MINUTES);
        return "验证码已发送";
    }

    @Override
    public Boolean register(String cellphone, String verifyCode, String userPassword) {

        if(cellphone==null || "".equals(cellphone)){
            throw new CustomException(ExceptionCode.CELLPHONE_INVALID);
        }

        if(userPassword==null || "".equals(userPassword)){
            throw new CustomException(ExceptionCode.PASSWORD_INVALID);
        }

        String verifyCodeInRedis = (String)redisTemplate.opsForValue().get(Constants.REGISTRY_CELLPHONE_VERIFYCODE + cellphone);
        if (!Objects.equals(verifyCode, verifyCodeInRedis)) {
            throw new CustomException(ExceptionCode.INVALID_KAPTCHA);
        }

        User user = userMapper.findByUserAccount(cellphone);
        if(user != null){
            throw new CustomException(ExceptionCode.CELLPHONE_REGISTERED);
        }


        //把密码加密
        String encodedPassword = Md5Utils.hash(userPassword + Constants.SALT);
        User user1 = new User();
        user1.setUserAccount(cellphone);
        user1.setUserPassword(encodedPassword);
        user1.setUserPhone(cellphone);

        userMapper.insert(user1);

        return true;

    }
}
